<template>
	<div class="icon-btn">
    <!-- 供应商店铺首页 -->
    <div class="icon-btn-item" v-if="!hideHome && !detailAll.is_close_item_shop && detailAll.supplier_index_link" @click="linkTo(detailAll.supplier_index_link)">
      <em class="fixed-shop"></em>
      <div>店铺</div>
    </div>
    <div class="icon-btn-item" v-else-if="!hideHome && !detailAll.is_close_item_shop" @click="linkTo(detailAll.index_link)">
      <em class="fixed-shop"></em>
      <div>店铺</div>
    </div>

    <!-- 定制  西安海师文化产业有限公司 start -->
    <template v-if="detailAll.item_detail_style_dz">
      <div class="icon-btn-item" @click="showSharePoster">
        <em class="fixed-share"></em>
        <div>分享</div>
      </div>
    </template>
    <!-- 定制  西安海师文化产业有限公司 end -->
    <template v-else>
      <div class="icon-btn-item" @click="clickCollect">
        <em class="fixed-collect" :class="isCollected?'collected':''"></em>
        <div>收藏</div>
      </div>
    </template>

    <div class="icon-btn-item cart-item" v-if="!hideCar" @click="JumpName('shop-car')">
      <em class="fixed-car"></em>
      <div>购物车</div>
      <b class="num" v-if="cartItemCount>0">{{cartItemCount}}</b>
    </div>

    <div class="icon-btn-item cart-item" v-if="!hideExchangeIcon" @click="JumpPath('/pointCart')">
      <em class="fixed-point"></em>
      <div>兑换清单</div>
      <b class="num" v-if="cartItemCount>=0">{{cartItemCount}}</b>
    </div>
    <goods-code :visible.sync="codeDialogVisible" :codeData="detailAll && detailAll.qrcode"></goods-code>
    <!-- ST 自定义分享 -->
    <god-share-cpt ref="god-share-cpt" @share="shareGoods"></god-share-cpt>
    <!-- ED 自定义分享 -->

    <!-- 授权弹出框 -->
    <small-login ref="smalllogin"></small-login>
  </div>
</template>

<script>
import Vue from 'vue'
  /**
   * @Author: yqe@360shop.cn
   * @Date: 2019-12-04 19:43:18
 * @Last Modified by: zhangmeng
 * @Last Modified time: 2020-07-31 16:54:32
   */
  import goodsCode from '../../../../components/goodsCode'
  import GodShareCpt from '@/components/god/god-share'
  import { addCollect, cancelCollectionGoods } from '@/api/user/member/memberApi'
  import { openPage } from '@/utils/utils.js'
  import SmallLogin from '@/components/SmallLogin/smallLogin'
  import small from '@/smallapp/small.js'
  import { wxItemQrCode } from '@/api/small/common'

  export default Vue.extend({
    name: 'index',
    components: {
      goodsCode,
      GodShareCpt,
      SmallLogin
    },
    props: {
      hideCar: {
        type: Boolean,
        default: false
      },
      hideHome: {
        type: Boolean,
        default: false
      },
      hideShare: {
        type: Boolean,
        default: false
      },
      hideExchangeIcon: {
        type: Boolean,
        default: true
      },
      detailAll: {
        type: Object,
        default: {}
      }
    },
    data() {
      return {
        codeDialogVisible: false,
        isCollected: false,
        cartItemCount: 0
      }
    },
    watch:{
      detailAll(val){
        this.cartItemCount = this.detailAll.cart_item_count
      }
    },
    created() {
      if (this.detailAll.time) {
        this.isCollected = true
      } else {
        this.isCollected = false
      }
      this.cartItemCount = this.detailAll.cart_item_count
      this.$Bus.$on('changeCartNum', (val) => {
        this.cartItemCount = Number(this.cartItemCount) + Number(val)
      })
    },
    mounted() {
      if (!small.checkLogin()) {
        this.$refs.smalllogin.openSmallLoginPop()
      }
    },
    beforeDestroy() {
      this.$Bus.$off('changeCartNum')
    },
    methods: {
      JumpPath(url, data) {
        this.$JumpPath(this, url, data)
      },
      JumpName(url, data) {
          if (!small.checkLogin()) {
            this.$refs.smalllogin.openSmallLoginPop()
            return false
          }
        this.$JumpName(this, url, data)
      },
      linkTo(url) {
        // TODO 小程序跳转
          openPage(url, 1)
      },
      // 商品二维码
      showGoodsCode() {
        this.$refs['god-share-cpt'].show(this.detailAll)
      },
      // 海报
      showSharePoster() {
        this.$refs['god-share-cpt'].onSelectItem({
          key: '2'
        })
      },
      shareGoods() {
        this.$loadingWrap.show()
        wxItemQrCode({item_id: this.detailAll.item_detail.item_id, activity: this.detailAll.activity,activityGroupId: this.$route.query.groupId}).then(res => {
          this.$loadingWrap.close()
          if (res.status == 1) {
            this.detailAll.qrcode = res.data.path
            this.detailAll.item_poster = res.data.item_poster
            this.goodsCodeDialogVisible = true;
          } else {
            this.$Error(res.msg)
          }
        })
      },
  
      // 点击收藏
      clickCollect() {
        if (!small.checkLogin()) {
          this.$refs.smalllogin.openSmallLoginPop()
          return false
        }
        const params = {
          id: this.detailAll.item_detail.item_id
        }
        if (this.isCollected) {
          cancelCollectionGoods(params).then(res => {
            if (res.status == 1) {
              this.isCollected = !this.isCollected
              // this.$Error('取消收藏成功')
            } else {
              this.$Error(res.msg)
            }
          })
        } else {
          addCollect(params).then(res => {
            if (res.status == 1) {
              this.isCollected = !this.isCollected
              this.$Error('收藏成功')
            } else {
              this.$Error(res.msg)
            }
          })
        }
      }
    }
  })
</script>

<style lang="scss">
  .icon-btn{
    display: flex;
    flex-direction: row;
    margin-right:20px;
    &-item{
      display:flex;
      flex-direction: column;
      justify-content: center;
      align-items:center;
      min-width: 90px;
      text-align: center;
      // margin-left: -8px;
      margin-right: 8px;
      font-size:20px;
      color:#333;
      em{
        width: 36px;
        height: 36px;
        display: inline-block;
        // margin-top: 4px;
        &.fixed-shop{
          background: url(https://img.wifenxiao.com/h5-wfx/images/item/detail/shop.png) center no-repeat;
          background-size: 100% auto;
        }
        &.fixed-collect{
          background: url(https://img.wifenxiao.com/h5-wfx/images/item/detail/collect1_1.png) center no-repeat;
          background-size: 100% auto;          
          &.collected{
            background-image:url("https://img.wifenxiao.com/h5-wfx/images/item/detail/collect2.png");
          }
        }
        &.fixed-share{
          background:url("https://wifenxiao.oss-cn-hangzhou.aliyuncs.com/h5-wfx/images/DZ/xahs/share.png") no-repeat;
          background-size: 100% auto;          
        }

        &.fixed-car{
          background: url(https://img.wifenxiao.com/h5-wfx/images/item/detail/cart2.png) center no-repeat;
          background-size: 100% auto;
        }
        &.fixed-point{
          background: url(https://img.wifenxiao.com/h5-wfx/images/user/point/icon_cart.png) center no-repeat;
          background-size: contain;
        }
      }
      >div{
        margin-top:6px;
      }
      &.cart-item{
        position:relative;
        .num{
          position:absolute;
          min-width:28px;
          height: 28px;
          padding:0 8px;
          top: -2px;
          right: 6px;
          line-height: 28px;
          background: #F30C23;
          font-size:20px;
          color: #fff;
          border-radius: 14px;
          font-weight: normal;
          text-align: center;
        }
      }
    }
  }
</style>
